//
//  YAYIShowSkillView.m
//  YAYIDoctor
//
//  Created by Veer on 16/5/12.
//  Copyright © 2016年 Veer. All rights reserved.
//

#import "YAYIShowSkillView.h"

#define leftMargin 15
#define topMargin 10
#define skillItemMargin 5
#define itemMinMargin 5
#define skillItemFont [UIFont systemFontOfSize:13]

@interface YAYISkillRowModel : NSObject

@property (nonatomic, assign) CGFloat centerMargin;
@property (nonatomic, strong) NSMutableArray *skillStrArray;

@end

@implementation YAYISkillRowModel

- (NSMutableArray *)skillStrArray
{
    if (nil == _skillStrArray) {
        _skillStrArray = [NSMutableArray array];
    }
    return _skillStrArray;
}

@end



@interface YAYIShowSkillView()

@property (nonatomic, strong) NSMutableArray *rowArray;


@end

@implementation YAYIShowSkillView

- (NSMutableArray *)rowArray
{
    if (nil == _rowArray) {
        _rowArray = [NSMutableArray array];
    }
    return _rowArray;
}


//- (NSArray *)skillArray
//{
//    if (nil == _skillArray) {
//        _skillArray = @[@"超声波洁齿美白", @"多生牙", @"覆盖义齿", @"非龋齿", @"复杂智齿", @"多颗牙种植", @"单颗牙齿种植", @"测试测试"];
//    }
//    
//    return _skillArray;
//}


- (void)setSkillArray:(NSArray *)skillArray
{
    _skillArray = skillArray;
    [self setupRow];
    [self setupSubView];
}


- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
//        [self setupRow];
//        [self setupSubView];
    }
    return self;
}


- (void)setupSubView
{
    for (int i = 0; i < self.rowArray.count; i++) {
        YAYISkillRowModel *rowModel = self.rowArray[i];
        
        CGFloat itemX = leftMargin;
        for (int j = 0; j < rowModel.skillStrArray.count; j++) {
            NSString *skillStr = rowModel.skillStrArray[j];
            CGSize skillSize = [skillStr sizeWithFont:skillItemFont];
            
            CGFloat itemW = skillSize.width + skillItemMargin * 2;
            CGFloat itemH = skillSize.height + skillItemMargin * 2;
            CGFloat itemY = topMargin + itemH * i + topMargin * i;
            
            UILabel *itemLbl = [[UILabel alloc] init];
            itemLbl.text = skillStr;
            itemLbl.textColor = YAYIColor(64, 212, 194);
            itemLbl.font = skillItemFont;
            itemLbl.textAlignment = NSTextAlignmentCenter;
            itemLbl.layer.borderWidth = 1.4;
            itemLbl.layer.borderColor = YAYIColor(64, 212, 194).CGColor;
            itemLbl.layer.masksToBounds = YES;
            itemLbl.layer.cornerRadius = 7;
            [self addSubview:itemLbl];
            itemLbl.frame = CGRectMake(itemX, itemY, itemW, itemH);
            
            itemX += rowModel.centerMargin + itemW;
        }
    }
}



- (void)setupRow
{
    CGFloat curWidth = 0;
    CGFloat preItemWidth = 0;
    for (int i = 0; i < self.skillArray.count; i++) {
        NSString *skillStr = [self.skillArray[i] name];
        CGSize skillSize = [skillStr sizeWithFont:skillItemFont];
        
        
        
        if (0 == curWidth) {
            YAYISkillRowModel *rowModel = [[YAYISkillRowModel alloc] init];
            rowModel.centerMargin = itemMinMargin * 2;
            [self.rowArray addObject:rowModel];
            
            curWidth += skillSize.width + skillItemMargin * 2;
        }else{
            curWidth += skillSize.width + skillItemMargin * 2 + itemMinMargin;
        }
        
        preItemWidth = skillSize.width + skillItemMargin * 2 + itemMinMargin;
        
        if (curWidth > SCREEN_width - leftMargin * 2) {
            // 计算 centerMargin
            YAYISkillRowModel *rowModel = [self.rowArray lastObject];
            CGFloat allMargin = SCREEN_width - 2 * leftMargin - curWidth + preItemWidth + (rowModel.skillStrArray.count - 1) * itemMinMargin;
            CGFloat centerW = allMargin / (rowModel.skillStrArray.count - 1);
            rowModel.centerMargin = centerW;
            
            i--;
            curWidth = 0;
        }else{
            YAYISkillRowModel *rowModel = [self.rowArray lastObject];
            [rowModel.skillStrArray addObject:skillStr];
        }
    }
}


/** 求高度 */
+ (CGFloat)getHightWiht:(NSArray *)itemArray
{
    if (0 == itemArray.count) {
        return 0;
    }
    
    NSInteger rowCount = 0;
    
    CGFloat curWidth = 0;
    for (int i = 0; i < itemArray.count; i++) {
        NSString *skillStr = [itemArray[i] name];
        CGSize skillSize = [skillStr sizeWithFont:skillItemFont];
        
        if (0 == curWidth) {
            
            rowCount++;
            curWidth += skillSize.width + skillItemMargin * 2;
        }else{
            curWidth += skillSize.width + skillItemMargin * 2 + itemMinMargin;
        }
        
        if (curWidth > SCREEN_width - leftMargin * 2) {
            i--;
            curWidth = 0;
        }
    }
    
    CGSize skillSize = [@"测试" sizeWithFont:skillItemFont];
    CGFloat height = (skillSize.height + skillItemMargin * 2 + topMargin) *rowCount + topMargin;
    
    return height;
}


@end
